package com.zhu.wx.strategy;

import com.zhu.wx.context.ApplicationContextUtils;
import com.zhu.wx.entity.RecMsg;
import com.zhu.wx.entity.ResMsg;
import com.zhu.wx.mapper.RecMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 策略代理
 * 作用：统一调用
 */
public class RecStrategyContext {
    /**
     * 封装了策略对象
     */
    private WxRecStrategy strategy = null;

    public RecStrategyContext(WxRecStrategy strategy) {
        this.strategy = strategy;
    }

    /**
     * 实现了策略可以互换的功能
     *
     * @param strategy
     */
    public void setStrategy(WxRecStrategy strategy) {
        this.strategy = strategy;
    }

    public ResMsg execute(RecMsg params) {
        //请求记录
        RecMsgMapper recMsgMapper = ApplicationContextUtils.getBean(RecMsgMapper.class);
        recMsgMapper.insert(params);
        //请求处理
        ResMsg resMsg = this.strategy.execute(params);
        if (resMsg == null){
            return null;
        }
        resMsg.setFromUserName(params.getToUserName());
        resMsg.setToUserName(params.getFromUserName());
        //结果返回
        return resMsg;
    }
}
